package com.baizhi.service.impl;

import com.baizhi.dao.EmpDao;
import com.baizhi.entity.Emp;
import com.baizhi.service.EmpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@Service
@Transactional
public class EmpServiceImpl implements EmpService {
    // 声明日志对象
    private static final Logger log = LoggerFactory.getLogger(EmpService.class);

    private EmpDao empDao;

    @Autowired
    public EmpServiceImpl(EmpDao empDao) {
        this.empDao = empDao;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Emp> findAll() {
        return empDao.findAll();
    }

    @Override
    public void save(Emp emp) {
        log.debug("姓名：{}", emp.getName());
        log.debug("工资：{}", emp.getSalary());
        log.debug("生日：{}", emp.getBirthday());
        empDao.save(emp);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Emp findById(Integer id) {
        return empDao.findById(id);
    }

    @Override
    public void edit(Emp emp) {
        empDao.edit(emp);
    }

    @Override
    public void delete(Integer id) {
        empDao.delete(id);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Map<Integer, String> findMap() {
        Map<Integer, String> result = new TreeMap<>();
        empDao.findMap().forEach((id, emp) -> {
            result.put(id, emp.getName());
        });
        return result;
    }
}
